#!/usr/bin/env bash
# Copyright (c) 2022 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -e
root_path=$1
packtool_jar_path=$2
output_jar=$3
toolchain=$4
out_dir="${root_path}/src/main/java/out/${toolchain}"
build_path=$(pwd)
java_collection=""
declare -a unpack_class=(
    com/alibaba/fastjson/annotation/JSONCreator.java
    com/alibaba/fastjson/annotation/JSONField.java
    com/alibaba/fastjson/annotation/JSONType.java
    com/alibaba/fastjson/JSONArray.java
    com/alibaba/fastjson/JSONAware.java
    com/alibaba/fastjson/JSONException.java
    com/alibaba/fastjson/JSON.java
    com/alibaba/fastjson/JSONObject.java
    com/alibaba/fastjson/JSONReader.java
    com/alibaba/fastjson/JSONStreamAware.java
    com/alibaba/fastjson/JSONStreamContext.java
    com/alibaba/fastjson/JSONWriter.java
    com/alibaba/fastjson/parser/deserializer/DefaultFieldDeserializer.java
    com/alibaba/fastjson/parser/DefaultJSONParser.java
    com/alibaba/fastjson/parser/deserializer/ExtraProcessable.java
    com/alibaba/fastjson/parser/deserializer/ExtraProcessor.java
    com/alibaba/fastjson/parser/deserializer/ExtraTypeProvider.java
    com/alibaba/fastjson/parser/deserializer/FieldDeserializer.java
    com/alibaba/fastjson/parser/deserializer/FieldTypeResolver.java
    com/alibaba/fastjson/parser/deserializer/ObjectDeserializer.java
    com/alibaba/fastjson/parser/deserializer/ParseProcess.java
    com/alibaba/fastjson/parser/deserializer/EnumDeserializer.java
    com/alibaba/fastjson/parser/Feature.java
    com/alibaba/fastjson/parser/deserializer/JavaBeanDeserializer.java
    com/alibaba/fastjson/util/JavaBeanInfo.java
    com/alibaba/fastjson/parser/deserializer/JavaObjectDeserializer.java
    com/alibaba/fastjson/parser/JSONLexer.java
    com/alibaba/fastjson/parser/JSONToken.java
    com/alibaba/fastjson/parser/deserializer/ArrayListTypeFieldDeserializer.java
    com/alibaba/fastjson/parser/ParseContext.java
    com/alibaba/fastjson/parser/deserializer/MapDeserializer.java
    com/alibaba/fastjson/parser/ParserConfig.java
    com/alibaba/fastjson/parser/deserializer/ResolveFieldDeserializer.java
    com/alibaba/fastjson/parser/SymbolTable.java
    com/alibaba/fastjson/parser/deserializer/ThrowableDeserializer.java
    com/alibaba/fastjson/PropertyNamingStrategy.java
    com/alibaba/fastjson/serializer/AfterFilter.java
    com/alibaba/fastjson/serializer/ObjectArrayCodec.java
    com/alibaba/fastjson/serializer/CharArrayCodec.java
    com/alibaba/fastjson/serializer/ArraySerializer.java
    com/alibaba/fastjson/serializer/BeforeFilter.java
    com/alibaba/fastjson/serializer/BigDecimalCodec.java
    com/alibaba/fastjson/serializer/BooleanCodec.java
    com/alibaba/fastjson/serializer/CollectionCodec.java
    com/alibaba/fastjson/serializer/DateCodec.java
    com/alibaba/fastjson/serializer/EnumSerializer.java
    com/alibaba/fastjson/serializer/FieldSerializer.java
    com/alibaba/fastjson/serializer/IntegerCodec.java
    com/alibaba/fastjson/serializer/JavaBeanSerializer.java
    com/alibaba/fastjson/serializer/JSONSerializable.java
    com/alibaba/fastjson/serializer/JSONSerializer.java
    com/alibaba/fastjson/serializer/ListSerializer.java
    com/alibaba/fastjson/serializer/MapSerializer.java
    com/alibaba/fastjson/serializer/MiscCodec.java
    com/alibaba/fastjson/serializer/NameFilter.java
    com/alibaba/fastjson/serializer/ObjectSerializer.java
    com/alibaba/fastjson/serializer/PropertyFilter.java
    com/alibaba/fastjson/serializer/PropertyPreFilter.java
    com/alibaba/fastjson/serializer/SerialContext.java
    com/alibaba/fastjson/serializer/SerializeConfig.java
    com/alibaba/fastjson/serializer/SerializeFilter.java
    com/alibaba/fastjson/serializer/SerializerFeature.java
    com/alibaba/fastjson/serializer/SerializeWriter.java
    com/alibaba/fastjson/serializer/SimplePropertyPreFilter.java
    com/alibaba/fastjson/serializer/StringCodec.java
    com/alibaba/fastjson/serializer/ValueFilter.java
    com/alibaba/fastjson/TypeReference.java
    com/alibaba/fastjson/util/FieldInfo.java
    com/alibaba/fastjson/util/IdentityHashMap.java
    com/alibaba/fastjson/util/ParameterizedTypeImpl.java
    com/alibaba/fastjson/util/TypeUtils.java
)
unpack_class_length=${#unpack_class[@]}
for ((i=0; i<${unpack_class_length};++i))
do
  java_collection="${java_collection} ./${unpack_class[$i]}"
done
cd "${root_path}/src/main/java"
compile_command="javac -source 1.8 -target 1.8  -d ${out_dir} ${java_collection}"
echo ${compile_command}
eval ${compile_command}
jar_path="${root_path}/jar/fastjson-1.2.83.jar"
manifest_path="${root_path}/src/main/java/META-INF/MANIFEST.MF"
cd ${out_dir}
product_unpack_jar_command="jar -cvfm ${jar_path} ${manifest_path} ./com"
if [ -d "${root_path}/jar" ]
    then
        echo "${root_path}/jar exist"
    else
        mkdir -p "${root_path}/jar"
fi

if [ -f ${jar_path} ]
    then
        echo "${jar_path} exist"
    else
        eval ${product_unpack_jar_command}
fi
cd ${root_path}
if [ -f "${packtool_jar_path}/fastjson-1.2.83.jar" ]
    then
        echo "${packtool_jar_path}/fastjson-1.2.83.jar exist"
    else
        cp ${jar_path} "${packtool_jar_path}/fastjson-1.2.83.jar"
fi

if [ -d "${build_path}/${output_jar}" ]
    then
        echo "${build_path}/${output_jar} exist"
    else
        mkdir -p "${build_path}/${output_jar}"
fi
cp ${jar_path} "${build_path}/${output_jar}"